import Question.Problem;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.sql.Time;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;

/**
 * Creat with IntelliJ IDEA
 * Description:考试系统界面搭建
 * Date:2021-12-14
 * Time:19:03
 */
public class testWindow implements ActionListener,Runnable{

    public static int number;
    public static Problem[] problems;
    public static String[] type = new String[]{"单选题","多选题","判断题"};

    JFrame frame = new JFrame();
    JLabel north = new JLabel();
    public static Box west,east,south,center;
    public static Font font = new Font("Dialog",0,26);//设置默认字体
    public static RandomAcquirement acquirement;

    public static timer timer;//计时器

    long time;//总用时
    long initialTime;//考试开始时间
    long newQuestionTime;//新题目开始时间
    long currentTime;//当前时间


//    public static long initialTime;//考试开始时间
    public static Thread getTime;//用于获取当前时间的线程

    int[][] score = new int[3][10];//记录个题目得分情况
    int totalScore;//记录总分

    JCheckBox[] c1 = new JCheckBox[4];
    JRadioButton[] c2 = new JRadioButton[4];

    public static void main(String[] args) {
        testWindow test = new testWindow();
    }

    public testWindow(){//构造方法


        //创建一个随机抽题系统
        acquirement = new RandomAcquirement();

        //创建一个计时器
        timer = new timer();
//        t.count(this);//计时器开始计时

        //记录考试起始时间
        initialTime = new Date().getTime();

        //创建线程
        getTime = new Thread(this);
        getTime.setName("getTime");
        getTime.start();



        //准备题目
        try {
            problems = new Problem[3];
            problems[0] = new Problem("E:\\方秋慧\\JAVA程序设计\\单选.txt",4);
            problems[1] = new Problem("E:\\方秋慧\\JAVA程序设计\\多选.txt",4);
            problems[2] = new Problem("E:\\方秋慧\\JAVA程序设计\\判断.txt",2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //设置界面

        InitGlobalFont(this.font);//调用InitGlobalFont()方法设置全局字体 - 避免重复设置字体
        frame.setTitle("单机版java简易机考程序");//设置窗口标题

        setNorth();

        //西边布局：总用时（总计时器）共有多少题，已经做的题
        west = Box.createVerticalBox();
//        JTextArea totalTime = new JTextArea("总用时");
//        west.add(totalTime);
        JTextArea amount = new JTextArea("已经做的题");
        west.add(amount);
        frame.add(west,BorderLayout.WEST);

        //东边布局：20秒倒计时显示
        east = Box.createVerticalBox();
        JTextArea timeRemaining = new JTextArea("剩余时间",1,10);//文本框 - 用于显示每一题剩余的时间
        east.add(timeRemaining);
        frame.add(east,BorderLayout.EAST);

        //南边布局：每一题的得分
        south = Box.createVerticalBox();
        setSouth();

        //中间布局：题目：单选、多选、判断、每一次提交显示正误、得分，&正确答案
        number = 0;
        center = Box.createVerticalBox();
        int t = acquirement.setType();//抽取题型
        int n = acquirement.setNumber();//抽题题号
        setCenter(t,n);

        frame.validate();
        frame.setBounds(30,30,1200,700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //设置中间布局
    public void setCenter(int t,int n){//题型 题号
        number++;
//        center.removeAll();
//        Box subject = Box.createHorizontalBox();
//        JLabel label = new JLabel(type[t]);//提示题型
//        subject.add(label);
        Box choice = Box.createVerticalBox();

        JTextArea q = new JTextArea("["+type[t]+"] "+number+"、"+problems[t].getQuestion(n).getQ());//设置题目描述
//        q.setSize(50,3);
        q.setBackground(null);
        q.setLineWrap(true);//设置自动换行
        q.setWrapStyleWord(true);//设置以单词为界换行
        q.setEditable(false);//设置不可编辑
        choice.add(q);
//        subject.add(q);
//        center.add(subject);

//        choice = Box.createVerticalBox();//盒式布局 - 存放选项
        String[] C = problems[t].getQuestion(n).getChoice();
        if (t==1){//多选
//            c1 = new JCheckBox[C.length];//多选列表
            for (int i = 0; i < C.length; i++){
                c1[i] = new JCheckBox(C[i]);//设置选项内容
                choice.add(c1[i]);
            }
        }else {
//            c2 = new JRadioButton[C.length];//单项列表
            ButtonGroup group = new ButtonGroup();//创建一个按钮组 - 同时只能选其中一个选项
            for (int i = 0; i < C.length; i++) {
                c2[i] = new JRadioButton(C[i]);//设置选项内容
                group.add(c2[i]);
                choice.add(c2[i]);
            }
            if (t==1){//多选
//            c1 = new JCheckBox[C.length];//多选列表
                for (int i = 0; i < C.length; i++){
                    c1[i] = new JCheckBox(C[i]);//设置选项内容
                    choice.add(c1[i]);
                }
            }else {
//            c2 = new JRadioButton[C.length];//单项列表
                ButtonGroup group = new ButtonGroup();//创建一个按钮组 - 同时只能选其中一个选项
                for (int i = 0; i < C.length; i++) {
                    c2[i] = new JRadioButton(C[i]);//设置选项内容
                    group.add(c2[i]);
                    choice.add(c2[i]);
                }
            }
        }

        JButton submit = new JButton("确定");//提交按钮
        submit.addActionListener(this);
        choice.add(submit);

        center.add(choice);
        frame.add(center,BorderLayout.CENTER);
    }

    //设置南边布局 - 答题数 答对数
    public void setSouth(){
        int[] count = acquirement.getTypeNum();
//        Box box = Box.createHorizontalBox();
        JPanel box = new JPanel();

        Box Score = Box.createVerticalBox();
        box.add(Score);
        JLabel total = new JLabel(totalScore+"");
        box.add(total);

        Box single = Box.createHorizontalBox();
        for (int i = 0; i < count[0]; i++) {
            Integer sc = score[0][i];
            JButton s = new JButton(sc.toString());//显示得分
            if (sc==0){//该题得分为0 - 回答错误
                s.setBackground(Color.red);//设为红色
            }else {//该题得分不为0 - 回答正确
                s.setBackground(Color.green);//设为绿色
            }
            single.add(s);
        }

        Box multiple = Box.createHorizontalBox();
        for (int i = 0; i < count[1]; i++) {
            Integer sc = score[1][i];
            JButton s = new JButton(sc.toString());//显示得分
            if (sc==0){//该题得分为0 - 回答错误
                s.setBackground(Color.red);//设为红色
            }else {//该题得分不为0 - 回答正确
                s.setBackground(Color.green);//设为绿色
            }
            multiple.add(s);
        }

        Box judgement = Box.createHorizontalBox();
        for (int i = 0; i < count[2]; i++) {
            Integer sc = score[2][i];
            JButton s = new JButton(sc.toString());//显示得分
            if (sc==0){//该题得分为0 - 回答错误
                s.setBackground(Color.red);//设为红色
            }else {//该题得分不为0 - 回答正确
                s.setBackground(Color.green);//设为绿色
            }
            judgement.add(s);
        }

        Score.add(single);
        Score.add(multiple);
        Score.add(judgement);

        south.add(box);
        frame.add(south,BorderLayout.SOUTH);
    }
//    public void setSouth(){
//        int[] count = acquirement.getTypeNum();
////        Box box = Box.createHorizontalBox();
//        JLabel box = new JLabel();
//
//        Box Score = Box.createVerticalBox();
//        box.add(Score);
//        JLabel total = new JLabel(totalScore+"");
//        box.add(total);
//
//        Box single = Box.createHorizontalBox();
//        for (int i = 0; i < count[0]; i++) {
//            Integer sc = score[0][i];
//            JButton s = new JButton(sc.toString());//显示得分
//            if (sc==0){//该题得分为0 - 回答错误
//                s.setBackground(Color.red);//设为红色
//            }else {//该题得分不为0 - 回答正确
//                s.setBackground(Color.green);//设为绿色
//            }
//            single.add(s);
//        }
//
//        Box multiple = Box.createHorizontalBox();
//        for (int i = 0; i < count[1]; i++) {
//            Integer sc = score[1][i];
//            JButton s = new JButton(sc.toString());//显示得分
//            if (sc==0){//该题得分为0 - 回答错误
//                s.setBackground(Color.red);//设为红色
//            }else {//该题得分不为0 - 回答正确
//                s.setBackground(Color.green);//设为绿色
//            }
//            multiple.add(s);
//        }
//
//        Box judgement = Box.createHorizontalBox();
//        for (int i = 0; i < count[2]; i++) {
//            Integer sc = score[2][i];
//            JButton s = new JButton(sc.toString());//显示得分
//            if (sc==0){//该题得分为0 - 回答错误
//                s.setBackground(Color.red);//设为红色
//            }else {//该题得分不为0 - 回答正确
//                s.setBackground(Color.green);//设为绿色
//            }
//            judgement.add(s);
//        }
//
//        Score.add(single);
//        Score.add(multiple);
//        Score.add(judgement);
//
//        south.add(box);
//        frame.add(south,BorderLayout.SOUTH);
//    }

    //设置北边布局
    public void setNorth(){
//        north.removeAll();
//        long currentTime = new Date().getTime();
//        long total = currentTime-initialTime;
//        int minutes = (int)total/1000/60;
//        int seconds = (int)total/1000;
//        JLabel totalTime = new JLabel("总用时："+minutes+"分"+seconds+"秒");
        JLabel totalTime = new JLabel("总用时："+getTime()+"秒");

        north.add(totalTime);
        frame.add(north,BorderLayout.NORTH);
    }

    public long getTime(){
        currentTime = new Date().getTime();
        time = (currentTime-initialTime)/1000;
        return time;
    }

    @Override
    public void run() {
        while(number<15){
            getTime();
//            try {
//                Thread.sleep(500);
//            }catch (InterruptedException e){
//                e.printStackTrace();
//            }
        }
////        while(Thread.currentThread().getName().equals("getTime")){
//            while(true){
//                setNorth();
//                try {
//                    Thread.sleep(900);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
////        }
    }

    public void countScore(){
        int t = acquirement.getType();
        int n = acquirement.getNumber();
        String answer = "";
        if (t==1){
            if (c1[0].isSelected()){
                answer += "A";
            }
            if (c1[1].isSelected()){
                if (answer.equals("")){
                    answer += "B";
                }else {
                    answer += "、"+"B";
                }
            }
            if (c1[2].isSelected()){
                if (answer.equals("")){
                    answer += "C";
                }else {
                    answer += "、"+"C";
                }
            }
            if (c1[3].isSelected()){
                if (answer.equals("")){
                    answer += "D";
                }else {
                    answer += "、"+"D";
                }
            }
        }else if (t==0){
            if (c2[0].isSelected()){
                answer += "A";
            }else if (c2[1].isSelected()){
                answer += "B";
            }else if (c2[2].isSelected()){
                answer += "C";
            }else if (c2[3].isSelected()){
                answer += "D";
            }
        }else {
            if (c2[0].isSelected()){
                answer += "A";
            }else if (c2[1].isSelected()){
                answer += "B";
            }
        }

        if (answer.equals(problems[t].getQuestion(n).getAnswer())){//答案正确
            if (t==1){//多选题得分
                score[t][n] = 2;
                totalScore += 2;
            }else {//单选或判断得分
                score[t][n] = 1;
                totalScore += 1;
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        t.timeRemain();
        //计算得分
        countScore();
        //更新得分
        setSouth();
        //继续出题
        newQuestionTime = new Date().getTime();

        if (number<15){//当题目未出完时
            int t = acquirement.setType();//抽取题型
            int n = acquirement.setNumber();//抽题题号
            setCenter(t,n);//刷新界面
            timer.count(this);
        }else {//题目出完 - 考试结束 - 统计总分
            JOptionPane.showMessageDialog(null,"考试结束，你的得分为："+totalScore,"恭喜你",JOptionPane.INFORMATION_MESSAGE);
        }
    }

    //修改swing全局字体 - 在main方法中生成其他GUI图形界面之前调用，可以将字体设置为我们传参的字体
    public static void InitGlobalFont(Font f){
        FontUIResource fontRes = new FontUIResource(f);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }

}
